package com.wjbgn.electronicnian.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wjbgn.electronicnian.convert.NianStealDumplingsLogDoConvert;
import com.wjbgn.electronicnian.dto.NianStealDumplingsLogDTO;
import com.wjbgn.electronicnian.dto.Result;
import com.wjbgn.electronicnian.entity.NianStealDumplingsLogDO;
import com.wjbgn.electronicnian.service.NianStealDumplingsLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description:
 * Create Date: 2022-01-21T17:38:21.072290700
 *
 * @author weirx
 * @version 1.0
 */
@Api(tags = "")
@RestController
@RequestMapping("/nianstealdumplingslog")
@AllArgsConstructor
public class NianStealDumplingsLogController {

    /**
     * NianStealDumplingsLogService
     */
    private NianStealDumplingsLogService nianStealDumplingsLogService;

    /**
     * 分页列表
     *
     * @param nianStealDumplingsLogDTO
     * @return
     */
    @ApiOperation(value = "分页列表")
    @GetMapping("/pageList")
    public Result pageList(NianStealDumplingsLogDTO nianStealDumplingsLogDTO) {
        QueryWrapper<NianStealDumplingsLogDO> queryWrapper = new QueryWrapper<>(NianStealDumplingsLogDoConvert.dtoToDo(nianStealDumplingsLogDTO));
        Page<NianStealDumplingsLogDO> page = new Page<>(nianStealDumplingsLogDTO.getPage(), nianStealDumplingsLogDTO.getLimit());
        Page<NianStealDumplingsLogDO> pageList = nianStealDumplingsLogService.page(page, queryWrapper);
        return Result.success(NianStealDumplingsLogDoConvert.pageConvert(pageList));
    }

    /**
     * 查看偷饺子的人
     *
     * @param nianId
     * @return
     */
    @ApiOperation(value = "list")
    @GetMapping("/list")
    public Result list(@RequestParam("nianId") long nianId) {
        // 最近偷我饺子的15个人
        List<NianStealDumplingsLogDO> list = nianStealDumplingsLogService.lambdaQuery()
                .select(NianStealDumplingsLogDO::getNianId,NianStealDumplingsLogDO::getWhoSteal,NianStealDumplingsLogDO::getDumplingsName)
                .eq(NianStealDumplingsLogDO::getNianId, nianId)
                .orderByDesc(NianStealDumplingsLogDO::getCreateTime)
                .last("limit 0,15")
                .list();
        // top5
        QueryWrapper<NianStealDumplingsLogDO> queryWrapper = new QueryWrapper<NianStealDumplingsLogDO>()
                .select("who_steal,count(id) as stealNum")
                .eq("nian_id", nianId)
                .groupBy("who_steal")
                .orderByDesc("count(id)")
                .last("limit 5");
        List<NianStealDumplingsLogDO> list1 = nianStealDumplingsLogService.list(queryWrapper);


        Map<String,Object> map = new HashMap<>(4);
        map.put("top5",list1);
        map.put("stealUser",list);
        return Result.success(map);
    }

    /**
     * 根据主键查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据主键查询")
    @GetMapping("/info/getById")
    public Result info(@RequestParam("id") Long id) {
        NianStealDumplingsLogDO nianStealDumplingsLog = nianStealDumplingsLogService.getById(id);
        return Result.success(null == nianStealDumplingsLog ? null : NianStealDumplingsLogDoConvert.doToDto(nianStealDumplingsLog));
    }

    /**
     * 新增
     *
     * @param nianStealDumplingsLogDTO
     * @return
     */
    @ApiOperation(value = "新增")
    @PostMapping("/save")
    public Result save(@RequestBody NianStealDumplingsLogDTO nianStealDumplingsLogDTO) {
        boolean flag = nianStealDumplingsLogService.save(NianStealDumplingsLogDoConvert.dtoToDo(nianStealDumplingsLogDTO));
        if (flag) {
            return Result.success();
        }
        return Result.failed();
    }

    /**
     * 更新
     *
     * @param nianStealDumplingsLogDTO
     * @return
     */
    @ApiOperation(value = "根据id更新")
    @PostMapping("/updateById")
    public Result update(@RequestBody NianStealDumplingsLogDTO nianStealDumplingsLogDTO) {
        boolean flag = nianStealDumplingsLogService.updateById(NianStealDumplingsLogDoConvert.dtoToDo(nianStealDumplingsLogDTO));
        if (flag) {
            return Result.success();
        }
        return Result.failed();
    }
}